package com.ttc.mBackup.BackupSMS;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.ttc.mBackup.BackupContacts.Contact;
import com.ttc.mBackup.BackupContacts.ContactDAO;
import com.ttc.mBackup.BackupContacts.Phone;


public class SMSStatistics {
	//Content for SMS , these may change in future SDK.
	private static final Uri SMS_CONTENT_URI = Uri.parse("content://sms");
	
	String tag = "read";
	
	private Context context;
	/*
	 * Constructor
	 * 
	 */
	public SMSStatistics(Context context){		
		super();
		this.context = context;
	}
	
	/*
	 * get sum of message on the phone
	 * @return: sum of message
	 */
	public int getCount() {
		Cursor cursor = context.getContentResolver().query(SMS_CONTENT_URI, null, null, null, null);
		return cursor.getCount();
	}
	
	/*
	 * Get Cursor of Message
	 */
	public Cursor getCursor() {
		return context.getContentResolver().query(SMS_CONTENT_URI, null, null, null, null);
	}
	
	/*
	 * Put message to the phone
	 */
	public void insertMessage(ContentValues values){
		context.getContentResolver().insert(SMS_CONTENT_URI, values);
	}

	/*
	 * Delete all message on the phone
	 */
	public void deleteAllMessage() {
		context.getContentResolver().delete(SMS_CONTENT_URI, null, null);
	}
	
	public ArrayList<String> ConvertContactNameToNumber(ArrayList<String> phonename){
		ArrayList<Contact> contact = new ContactDAO(context.getContentResolver()).readAllContacts();
		ArrayList<String> name = new ArrayList<String>();
		ArrayList<ArrayList<Phone>> phone = new ArrayList<ArrayList<Phone>>();
		ArrayList<String> phoneSelected = new ArrayList<String>();
		name.add(0, "");
		if(phonename.get(0).equals("Check all")){
			phoneSelected.add(0, "Check all");
			return phoneSelected;
		}
		phoneSelected.add(0, "Uncheck all");
		
		for (int i = 0; i < contact.size(); i++) {
			name.add(i + 1, contact.get(i).getStructuredName().getDisplayName());
			phone.add(i, contact.get(i).getPhones());
		}
		for (int j = 0; j < phonename.size(); j++) {
			for (int i = 1; i < name.size(); i++) {
				if(name.get(i).equals(phonename.get(j))){
					Log.v("check", "OK");
						for (int w = 0; w < phone.get(i - 1).size(); w++) {
							phoneSelected.add(j + 1, phone.get(i - 1).get(w).getNumber());
						}
				}
			}
		}
		for (int i = 1; i < phoneSelected.size(); i++) {
			String temp = phoneSelected.get(i).replaceAll("-", "").trim();
			phoneSelected.set(i, temp);
//			Log.v("check", phoneSelected.get(i));
		}
		return phoneSelected;
	}
	
}